package com.mujin.keji.collectionsystem.widget;

import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mujin.keji.collectionsystem.R;
import com.mujin.keji.collectionsystem.dialog.BaseNiceDialog;
import com.mujin.keji.collectionsystem.dialog.NiceDialog;
import com.mujin.keji.collectionsystem.dialog.ViewConvertListener;
import com.mujin.keji.collectionsystem.dialog.ViewHolder;
import com.mujin.keji.collectionsystem.module.outboundtask.adapter.ConcactAdapter;
import com.mujin.keji.collectionsystem.module.outboundtask.dto.ContactDto;
import com.mujin.keji.collectionsystem.module.outboundtask.views.OneCallActivity;
import com.mujin.keji.collectionsystem.utils.EmptyUtils;

import java.util.List;

/**
 * 打电话对话框
 *
 * @author weishixiong
 * @Time 2018-05-25
 */

public class CallPhoneDailog implements ConcactAdapter.CallDialogListener {
    private FragmentActivity context;
    private CallListener listener;
    private List<ContactDto> lists;
    private LinearLayoutManager linearLayoutManager;
    BaseNiceDialog mDialog;
    String caseId;

    public CallPhoneDailog(FragmentActivity context, List<ContactDto> lists, String caseId) {
        this.context = context;
        this.listener = listener;
        this.lists = lists;
        this.caseId = caseId;
        linearLayoutManager = new LinearLayoutManager(context);
    }

    @Override
    public void callDialog() {
        if (EmptyUtils.isNotEmpty(mDialog)) {
            mDialog.dismiss();
        }
    }

    public interface CallListener {

    }

    private ImageView ivAnHint;
    private TextView tvInputPhone;
    private ImageView ivDeletePhone;
    private RecyclerView recyclerview;
    private RelativeLayout rlOne;
    private RelativeLayout rlTwo;
    private RelativeLayout rlThree;
    private LinearLayout rlFour;
    private RelativeLayout rlFive;
    private RelativeLayout rlSix;
    private LinearLayout rlSeven;
    private RelativeLayout rlEight;
    private RelativeLayout rlNine;
    private RelativeLayout rlLeft;
    private RelativeLayout rlZero;
    private RelativeLayout rlRight;
    private TextView tvCall;
    private TextView tvOne;
    private TextView tvTwo;
    private TextView tvThree;
    private TextView tvFour;
    private TextView tvFive;
    private TextView tvSix;
    private TextView tvSeven;
    private TextView tvEight;
    private TextView tvNine;
    private TextView tvZero;

    /**
     * 关闭 打电话对话框
     *
     * @param
     */
    public void dismissCallDialog() {
        if (EmptyUtils.isNotEmpty(mDialog)) {
            mDialog.dismiss();
        }
    }

    /**
     * 打开 打电话对话框
     *
     * @param
     */
    public void showCallDialog() {
        NiceDialog.init()
                .setLayoutId(R.layout.dialog_select_concat)
                .setConvertListener(new ViewConvertListener() {
                    @Override
                    public void convertView(ViewHolder holder, BaseNiceDialog dialog) {
                        mDialog = dialog;
                        ivAnHint = (ImageView) holder.getView(R.id.iv_an_hint);
                        tvInputPhone = (TextView) holder.getView(R.id.tv_input_phone);
                        ivDeletePhone = (ImageView) holder.getView(R.id.iv_delete_phone);
                        recyclerview = (RecyclerView) holder.getView(R.id.recyclerview);
                        rlOne = (RelativeLayout) holder.getView(R.id.rl_one);
                        rlTwo = (RelativeLayout) holder.getView(R.id.rl_two);
                        rlThree = (RelativeLayout) holder.getView(R.id.rl_three);
                        rlFour = (LinearLayout) holder.getView(R.id.rl_four);
                        rlFive = (RelativeLayout) holder.getView(R.id.rl_five);
                        rlSix = (RelativeLayout) holder.getView(R.id.rl_six);
                        rlSeven = (LinearLayout) holder.getView(R.id.rl_seven);
                        rlEight = (RelativeLayout) holder.getView(R.id.rl_eight);
                        rlNine = (RelativeLayout) holder.getView(R.id.rl_nine);
                        rlLeft = (RelativeLayout) holder.getView(R.id.rl_left);
                        rlZero = (RelativeLayout) holder.getView(R.id.rl_zero);
                        rlRight = (RelativeLayout) holder.getView(R.id.rl_right);
                        tvCall = (TextView) holder.getView(R.id.tv_call);
                        tvOne = (TextView) holder.getView(R.id.tv_one);
                        tvTwo = (TextView) holder.getView(R.id.tv_two);
                        tvThree = (TextView) holder.getView(R.id.tv_three);
                        tvFour = (TextView) holder.getView(R.id.tv_four);
                        tvFive = (TextView) holder.getView(R.id.tv_five);
                        tvSix = (TextView) holder.getView(R.id.tv_six);
                        tvSeven = (TextView) holder.getView(R.id.tv_seven);
                        tvEight = (TextView) holder.getView(R.id.tv_eight);
                        tvNine = (TextView) holder.getView(R.id.tv_nine);
                        tvZero = (TextView) holder.getView(R.id.tv_zero);
                        recyclerview.setLayoutManager(linearLayoutManager);
                        ConcactAdapter concactAdapter = new ConcactAdapter(context, lists, CallPhoneDailog.this);
                        recyclerview.setAdapter(concactAdapter);
                        ivAnHint.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                mDialog.dismiss();
                            }
                        });
                        ivDeletePhone.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {

                                if (EmptyUtils.isNotEmpty(tvInputPhone.getText().toString())) {
                                    // s = s.substring(0,s.length() - 1);
                                    tvInputPhone.setText(tvInputPhone.getText().toString().substring(0, tvInputPhone.getText().toString().length() - 1));
                                } else {
                                    return;
                                }
                            }
                        });
                        tvCall.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if (TextUtils.isEmpty(tvInputPhone.getText().toString())) {
                                    return;
                                } else {
                                    mDialog.dismiss();
                                    //打电话
                                    Intent intent = new Intent(context, OneCallActivity.class);
                                    intent.putExtra("phoneNumber", tvInputPhone.getText().toString());
                                    intent.putExtra("caseId", String.valueOf(caseId));
                                    context.startActivity(new Intent(intent));
                                }
                            }
                        });
                        rlOne.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                tvInputPhone.setText(tvInputPhone.getText().toString() + tvOne.getText().toString());
                            }
                        });
                        rlTwo.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                tvInputPhone.setText(tvInputPhone.getText().toString() + tvTwo.getText().toString());
                            }
                        });
                        rlThree.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                tvInputPhone.setText(tvInputPhone.getText().toString() + tvThree.getText().toString());
                            }
                        });
                        rlFour.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                tvInputPhone.setText(tvInputPhone.getText().toString() + tvFour.getText().toString());
                            }
                        });
                        rlFive.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                tvInputPhone.setText(tvInputPhone.getText().toString() + tvFive.getText().toString());
                            }
                        });
                        rlSix.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                tvInputPhone.setText(tvInputPhone.getText().toString() + tvSix.getText().toString());
                            }
                        });
                        rlSeven.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                tvInputPhone.setText(tvInputPhone.getText().toString() + tvSeven.getText().toString());
                            }
                        });
                        rlEight.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                tvInputPhone.setText(tvInputPhone.getText().toString() + tvEight.getText().toString());
                            }
                        });
                        rlNine.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                tvInputPhone.setText(tvInputPhone.getText().toString() + tvNine.getText().toString());
                            }
                        });
                        rlZero.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                tvInputPhone.setText(tvInputPhone.getText().toString() + tvZero.getText().toString());
                            }
                        });
                        rlLeft.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                tvInputPhone.setText(tvInputPhone.getText().toString() + "*");
                            }
                        });
                        rlRight.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                tvInputPhone.setText(tvInputPhone.getText().toString() + "#");
                            }
                        });

                    }
                }).
                setShowBottom(true)
                .show(context.getSupportFragmentManager());
    }


}
